# import sys
# class Solution:
#     def hasCycle(self, head: ListNode) -> bool:
#         node=head
#         while node is not None:
#             if node.val==sys.maxsize:
#                 return True
#             node.val=sys.maxsize
#             node=node.next
#         return False

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        turtle=rabbit=head
        while rabbit and rabbit.next:
            rabbit=rabbit.next.next
            turtle=turtle.next
            if rabbit==turtle:
                return True
        return False